package com.geovis.boot.service.exception;

import com.geovis.boot.exception.CheckException;
import com.geovis.boot.exception.base.BaseResponseEntityExceptionHandler;
import com.geovis.boot.exception.base.ErrorMap;
import com.geovis.boot.exception.base.ErrorResponse;
import com.geovis.boot.exception.base.ErrorResponseFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;


/**
 *
 * Created by l.wang on 2021年1月3日
 */
@ControllerAdvice
public class DemoHandler extends BaseResponseEntityExceptionHandler {

    private MessageSource messageSource;

    @Autowired
    public DemoHandler(ErrorResponseFactory errorResponseFactory, MessageSource messageSource){
        super(errorResponseFactory);
        this.messageSource = messageSource;
    }

    @Override
    protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
        return new ResponseEntity<>(body, headers, status);
    }

    @ExceptionHandler(value = {CheckException.class})
    public ResponseEntity<Object> businessMessageException(CheckException ex, WebRequest request){
        ErrorResponse bodyOfResponse = errorResponseFactory.getErrorResponse(ErrorMap.CUSTOM_EXCEPTION_RETURN, request.getLocale(),ex.getMessage());
        return new ResponseEntity<>(bodyOfResponse, new HttpHeaders(), HttpStatus.METHOD_FAILURE);
    }

    @ExceptionHandler(value = {UserException.class})
    public ResponseEntity<Object> handleCheckVerifyCodeErrorException(UserException ex, WebRequest request) {

        ErrorResponse bodyOfResponse = errorResponseFactory.getErrorResponse(ErrorMap.ERROR_ALL_CLIENT_NO_SUCH_REQUEST_HANDLING_METHOD_ERROR, request.getLocale());
        return new ResponseEntity<>(bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST);
    }
}